/**
 *  Copyright 23.07.2012 oliver.keusemann@googlemail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package de.xelerity.geolessi.types.verbindung;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.xelerity.helper.StringFunktionen;

public class Linien extends ArrayList<Linie> implements Serializable {

	private static final long serialVersionUID = -1273903781871437629L;

	public void sort() {
		Collections.sort(this, new LinieComperator());
	}

	public boolean Contains(Linie pLinie, boolean pIncludesDirections) {
		if (pLinie == null) {
			return false;
		}
		Linien LN = new Linien();
		LN.add(pLinie);
		return Contains(LN, pIncludesDirections);
	}

	public Linie get(long LinienTyp, String LinienNummer) {

		for (Linie L : this) {
			if (L.getLinienTyp() == LinienTyp && L.getNummer().equalsIgnoreCase(LinienNummer)) {
				return L;
			}
		}
		return null;
	}

	public boolean Contains(Linien pLinien, boolean pIncludesDirections) {
		if (pLinien == null) {
			return false;
		}
		String LThisStr, LStr, LThisStr1, LStr1;

		// check if all pLinien are in THIS
		for (Linie L : pLinien) {
			LStr1 = L.getLinienTypAsString() + L.getNummer();
			LStr = "";
			if (pIncludesDirections) {
				for (Richtung dirs : L.getDirections()) {
					LStr = LStr + dirs.getDirection();
				}
			}
			LStr = LStr1 + StringFunktionen.Fuzziator(LStr);

			// Look in THIS for LStr
			LThisStr = "";
			boolean found = false;
			for (Linie LThis : this) {
				LThisStr1 = LThis.getLinienTypAsString() + LThis.getNummer();
				LThisStr = "";
				if (pIncludesDirections) {
					for (Richtung dirs : LThis.getDirections()) {
						LThisStr = LThisStr + dirs.getDirection();
					}
				}
				LThisStr = LThisStr1 + StringFunktionen.Fuzziator(LThisStr);

				if (LThisStr.equalsIgnoreCase(LStr)) {
					found = true;
					break;
				}
			}

			if (!found) {
				return false;
			}
		}
		return true;
	}

	public String toString() {
		String S = "";
		for (Linie L : this) {
			S = S + L.getLinienTypAsString() + " " + L.getNummer() + ",";
		}
		return "[" + S.replaceAll(",$\\s*", "") + "]";
	}

	public void print() {
		for (Linie L : this) {
			System.out.println("================================================================================================");
			System.out.println("Line: " + L.getLinienTypAsString() + " " + L.getNummer());
			System.out.println("------------------------------------------------------------------------------------------------");
			for (Richtung dirs : L.getDirections()) {
				System.out.println("-> " + dirs.toString());
			}
		}
	}

	public static void main(String[] args) {

		String subjectString = "U9v67678";
		StringBuffer resultString = new StringBuffer();
		try {
			Pattern regex = Pattern.compile("([0-9]+)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
			Matcher regexMatcher = regex.matcher(subjectString);
			while (regexMatcher.find()) {
				regexMatcher.appendReplacement(resultString, StringFunktionen.minLenRight("$1", 8, ' '));
			}
			regexMatcher.appendTail(resultString);
			System.out.println(resultString);
		} catch (Throwable ex) {
			// Syntax error in the regular expression
		}

	}
}
